package ldh.im.web.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.business.pojo.ImUser;
import ldh.im.business.pojo.where.ImUserWhere;
import ldh.im.web.service.ImUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2021-06-06 10:17:43
*/
@Controller
@RequestMapping("imUser")
public class ImUserController  {

	@Resource(name = "imUserService")
	private ImUserService imUserService;
	
	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	}

	@RequiresPermissions("imuser:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute ImUser imUser) throws Exception {
    	Objects.requireNonNull(imUser);
    	if (imUser.getId() == null) {
			imUserService.insert(imUser);
    	} else {
			imUserService.updateById(imUser);
    	}
    	return "redirect:/imUser/list";
    }

	@RequiresPermissions("imuser:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute ImUserWhere imUser) throws Exception {
    	Objects.requireNonNull(imUser);
    	if (imUser.getId() == null) {
			imUserService.insert(imUser);
		} else {
			imUserService.updateById(imUser);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("imuser:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}/{tenantId}")
    public String toEdit(@PathVariable Long id, @PathVariable Long tenantId, Model model) throws Exception {
		ImUser imUser = imUserService.getByIdAndTenantId(id, tenantId);
    	model.addAttribute("imUser", imUser);
		model.addAttribute("active", "imUser");
        return "imUser/imUserEdit";
    }

	@RequiresPermissions("imuser:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("active", "imUser");
        return "imUser/imUserEdit";
    }

	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}/{tenantId}")
    public String view(@PathVariable Long id, @PathVariable Long tenantId, Model model) throws Exception {
		Objects.requireNonNull(id);
		ImUser imUser = imUserService.getByIdAndTenantId(id, tenantId);
    	model.addAttribute("imUser", imUser);
		model.addAttribute("active", "imUser");
        return "imUser/imUserView";
    }

	@RequiresPermissions("imuser:view")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}/{tenantId}")
    public String viewJson(@PathVariable Long id, @PathVariable Long tenantId, Model model) throws Exception {
		Objects.requireNonNull(id);
		ImUser imUser = imUserService.getByIdAndTenantId(id, tenantId);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imUser)
				.toJson();
    }

	@RequiresPermissions("imuser:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute ImUserWhere imUserWhere, Model model) {
		imUserWhere.setOrder("id desc");
		PageResult<ImUser> imUsers = imUserService.findByImUserWhere(imUserWhere);
		model.addAttribute("imUsers", imUsers);
		model.addAttribute("active", "imUser");
		model.addAttribute("imUser", imUserWhere);
		return "imUser/imUserList";
	}

	@RequiresPermissions("imuser:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute ImUserWhere imUserWhere, Model model) {
		imUserWhere.setOrder("id desc");
		PageResult<ImUser> imUsers = imUserService.findByImUserWhere(imUserWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", imUsers)
				.toJson();
	}

	@RequiresPermissions("imuser:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}/{tenantId}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id, @PathVariable Long tenantId) {
		ImUser imUser = imUserService.getByIdAndTenantId(id, tenantId);
        Objects.requireNonNull(imUser);
		imUserService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}